import { _decorator, Component, Layers, Node, resources, Sprite, SpriteAtlas, tween, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CardsFlipToFrontOrBack')
export class CardsFlipToFrontOrBack extends Component {
    /**
     * 
     * @param thatNode :该翻转节点
     * @returns 
     */
    public flipToFront(thatNode: Node|null): Promise<void> {
        return this.flipToFrontOrBack(thatNode, false)
    }

    public flipToBack(thatNode: Node|null): Promise<void> {
        return this.flipToFrontOrBack(thatNode, true)
    }
    private flipToFrontOrBack(thatNode: Node|null, isBack: boolean=false) {
        return new Promise<void>(res => {
            const duration = 0.2;
            tween(thatNode)
                .to(duration, { eulerAngles: new Vec3(0, -90, 0) })
                .call(() => {
                    
                    if (isBack){
                        let sprite = thatNode.getComponent(Sprite)
                        resources.load("cards/0/cards", SpriteAtlas, (err, atlas) => {
                            const frame = atlas.getSpriteFrame('card_02');//修改成你的反面牌名称
                            sprite.spriteFrame = frame;
                        });
                        thatNode.layer = Layers.Enum.UI_2D
                    }else {
                        //这里就调用你要翻转正面的牌
                        let sprite = thatNode.getComponent(Sprite)
                        resources.load("cards/0/cards", SpriteAtlas, (err, atlas) => {
                            const frame = atlas.getSpriteFrame('card_307');
                            sprite.spriteFrame = frame;
                        });
                        thatNode.layer = Layers.Enum.UI_2D
                    }
                })
                .to(duration, { eulerAngles: new Vec3(0, 0, 0) })
                .call(res)
                .start();
        });
    }
}


